package com.example.my_musicbox;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;


public class MusicService extends Service {
    //添加日志的TAG常量
    private static final String TAG = "MusicService";
    MyReceiver serviceReceiver;
    AssetManager am;
    String[] musics = new String[]{
            "mojito.mp3",
            "secret_base.mp3",
            "i_want_to_love_this_world.mp3"};
    MediaPlayer mPlayer;

    //当前状态，1表示没有播放，2代表正在播放，3代表暂停
    int status = 1;
    //记录当前正在播放的音乐
    int current = 0;

    @Override
    public void onCreate() {
        //打印日志信息
        Log.d(TAG, "MusicService调用onCreate()...");
        super.onCreate();
        am = getAssets();

        //创建BroadcastReceiver
        serviceReceiver = new MyReceiver();

        //创建IntentFilter(过滤器)
        IntentFilter filter = new IntentFilter();
        filter.addAction(MainActivity.CTL_ACTION);
        registerReceiver(serviceReceiver, filter);

        //创建MediaPlayer
        mPlayer = new MediaPlayer();

        //为MediaPlayer播放完成事件绑定监听器
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                current++;
                //current播放的歌曲序号
                if (current >= 3) {
                    current = 0;
                }

                //发送广播通知Activity更改文本框
                Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
                sendIntent.putExtra("current", current);

                //发送广播，将被Activity组件中的BroadcastReceiver接收
                sendBroadcast(sendIntent);

                //准备播放音乐
                prepareAndPlay(musics[current]);
            }
        });
    }

    //绑定式服务调用onBind()方法
    @Override
    public IBinder onBind(Intent intent) {
        //打印日志信息
        Log.d(TAG, "MusicService调用onBind()...");

        return null;
    }

    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //打印日志信息
            Log.d(TAG, "MusicService调用onReceive()...");

            int control = intent.getIntExtra("control", -1);
            switch (control) {
                //播放或暂停
                case 1:
                    //原来处于没有播放状态
                    if (status == 1) {
                        //准备并播放音乐
                        //打印日志信息
                        Log.d(TAG, "MusicService准备并播放音乐...");
                        prepareAndPlay(musics[current]);
                        status = 0x12;
                    }
                    //原来处于播放状态
                    else if (status == 2) {
                        //暂停
                        //打印日志信息
                        Log.d(TAG, "MusicService音乐暂停...");
                        mPlayer.pause();
                        //改变为暂停状态
                        status = 0x13;
                    }
                    //原来处于暂停状态
                    else if (status == 3) {
                        //播放
                        //打印日志信息
                        Log.d(TAG, "MusicService音乐播放...");
                        mPlayer.start();
                        //改变状态
                        status = 0x12;
                    }
                    break;
                //停止声音

                case 2:
                    // 如果原来正在播放或暂停
                    if (status == 2 || status == 3) {
                        // 停止播放
                        //打印日志信息
                        Log.d(TAG, "MusicService音乐停止...");
                        mPlayer.stop();
                        status = 1;
                    }
                    break;

                //播放上一首
                case 3:
                    //原来处于没有播放或暂停状态
                    if (status == 1 || status == 3) {
                        if (current == 0) {
                            current = 3;
                            //打印日志信息
                            Log.d(TAG, "MusicService准备并播放音乐...");
                            prepareAndPlay(musics[current]);
                        }
                        //准备并播放音乐
                        else {
                            current = current - 1;
                            //打印日志信息
                            Log.d(TAG, "MusicService准备并播放音乐...");
                            prepareAndPlay(musics[current]);
                        }
                        status = 2;
                    }
                    //原来处于播放状态
                    else if (status == 2) {
                        //上一首//准备并播放音乐
                        if (current == 0) {
                            current = 3;
                            //打印日志信息
                            Log.d(TAG, "MusicService准备并播放音乐...");
                            prepareAndPlay(musics[current]);
                        } else {
                            current = current - 1;
                            //打印日志信息
                            Log.d(TAG, "MusicService准备并播放音乐...");
                            prepareAndPlay(musics[current]);
                        }
                    }
                    break;

                //播放下一首
                case 4:
                    //原来处于没有播放或暂停状态
                    if (status == 1 || status == 3) {
                        if (current == 3) {
                            current = 0;
                            //准备并播放音乐
                            //打印日志信息
                            Log.d(TAG, "MusicService准备并播放音乐...");
                            prepareAndPlay(musics[current]);
                        }
                        else {
                            current = current + 1;
                            //打印日志信息
                            Log.d(TAG, "MusicService准备并播放音乐...");
                            prepareAndPlay(musics[current]);
                        }
                        status = 2;
                    }
                    //原来处于播放状态
                    else if (status == 2) {
                        //下一首
                        if (current == 3) {
                            current = 0;
                            //打印日志信息
                            Log.d(TAG, "MusicService准备并播放音乐...");
                            prepareAndPlay(musics[current]);
                        } else {
                            current = current + 1;
                            //打印日志信息
                            Log.d(TAG, "MusicService准备并播放音乐...");
                            prepareAndPlay(musics[current]);
                        }
                    }
                    break;
            }

            //广播通知Activity更改图标、文本框
            Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
            sendIntent.putExtra("update", status);
            sendIntent.putExtra("current", current);

            //发送广播，将被Activity组件中的BroadcastReceiver接收
            sendBroadcast(sendIntent);
        }
    }

    private void prepareAndPlay(String music) {
        //打印日志信息
        Log.d(TAG, "MusicService调用prepareAndPlay()...");

        try {
            //打开指定音乐文件
            AssetFileDescriptor afd = am.openFd(music);
            mPlayer.reset();
            //使用MediaPlayer加载指定的音乐文件
            mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            //准备声音
            mPlayer.prepare();
            //播放
            mPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
